package com.example.origin.flowable.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import lombok.experimental.UtilityClass;

import java.util.*;

@UtilityClass
public class JsonPrintUtil {

    public <T> String jsonStr(T deploy) {
        Map<String, Object> stringObjectMap = getMap(deploy);
        return JSONUtil.toJsonPrettyStr(stringObjectMap);
    }

    private static <T> Map<String, Object> getMap(T deploy) {
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(deploy, new HashMap<>(), CopyOptions.create().ignoreError());
        Set<String> set = new HashSet<>();
        for (Map.Entry<String, Object> stringObjectEntry : stringObjectMap.entrySet()) {
            Object value = stringObjectEntry.getValue();
            if (ObjectUtil.isBasicType(value) || value instanceof CharSequence || value instanceof Date) {
                //System.out.println(stringObjectEntry.getKey());
                continue;
            }
            set.add(stringObjectEntry.getKey());
        }
        for (String s : set) {
            stringObjectMap.remove(s);
        }
        return stringObjectMap;
    }

    public <T> String jsonStr(List<T> list) {
        List<Map<String, Object>> objs = new ArrayList<>();
        for (T t : list) {
            Map<String, Object> stringObjectMap = getMap(t);
            objs.add(stringObjectMap);
        }
        return JSONUtil.toJsonPrettyStr(objs);
    }
}
